﻿using System.Collections.Generic;
using FlickrNet;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using WroxGallery.Models.Implementations;
using WroxGallery.Models.Interfaces;
using PhotoCollection=WroxGallery.Models.PhotoCollection;
using System.Collections.Specialized;

namespace WroxGallery.Tests.Models
{
    /// <summary>
    /// Summary description for FlickrGalleryRepository
    /// </summary>
    [TestClass]
    public class FlickrGalleryRepositoryTest
    {
        private FlickrGalleryRepository repository;
        private IConfigurationRepository configurationRepository;
        private IFlickrAPI flickrAPIWrapper;

        private MockRepository mocks;

        [TestInitialize]
        public void InizializeRepository()
        {    
            mocks = new MockRepository();
            configurationRepository = mocks.DynamicMock<IConfigurationRepository>();
            flickrAPIWrapper = mocks.DynamicMock<IFlickrAPI>();
        }

        [TestMethod]
        public void FlickrGalleryRepository_Can_Get_List_Of_Albums()
        {
            using (mocks.Record())
            {
                Expect.Call(configurationRepository.FickerUserName).Return("userName");
                Expect.Call(flickrAPIWrapper.PhotosetsGetList("userName")).Return(BuildPhotoSetList(10));
            }
            repository = new FlickrGalleryRepository(configurationRepository, flickrAPIWrapper);


            using (mocks.Playback())
            {
                var albums = repository.GetAlbums(5);
                Assert.AreEqual(5, albums.Count);
            }
        }

        [TestMethod]
        public void FlickrGalleryRepository_Can_Get_List_Of_Photo()
        {
            using (mocks.Record())
            {
                Expect.Call(configurationRepository.FickerUserName).Return("userName");
                Expect.Call(flickrAPIWrapper.PhotosSearch("userName",5)).Return(BuildPhotoList(5));
                Expect.Call(flickrAPIWrapper.PhotosGetInfo("123")).Return(new PhotoInfo(){Description = "Description"}).Repeat.Times(5);
            }
            repository = new FlickrGalleryRepository(configurationRepository, flickrAPIWrapper);

            using (mocks.Playback())
            {
                PhotoCollection collection = repository.GetLatestPhoto(5);
                Assert.AreEqual(5, collection.Count);
            }
        }

        [TestMethod]
        public void FlickrGalleryRepository_Can_Get_List_Of_Five_Photo_From_Album()
        {
            using (mocks.Record())
            {
                Expect.Call(flickrAPIWrapper.PhotosetsGetPhotos("abc", 5)).Return(BuildPhotoset(5));
                Expect.Call(flickrAPIWrapper.PhotosGetInfo("123")).Return(new PhotoInfo() { Description = "Description" }).Repeat.Times(5);
            }
            repository = new FlickrGalleryRepository(configurationRepository, flickrAPIWrapper);

            using (mocks.Playback())
            {
                PhotoCollection collection = repository.GetPhotosInAlbum("abc", 5);
                Assert.AreEqual(5, collection.Count);
            }
        }

        [TestMethod]
        public void FlickrGalleryRepository_Can_Get_List_Of_Five_Photo_Searching_By_Tag()
        {
            using (mocks.Record())
            {
                Expect.Call(configurationRepository.FickerUserName).Return("userName");
                Expect.Call(flickrAPIWrapper.PhotosSearch("userName", "abc", 5)).Return(BuildPhotoList(5));
                Expect.Call(flickrAPIWrapper.PhotosGetInfo("123")).Return(new PhotoInfo() { Description = "Description" }).Repeat.Times(5);
            }
            repository = new FlickrGalleryRepository(configurationRepository, flickrAPIWrapper);

            using (mocks.Playback())
            {
                PhotoCollection collection = repository.SearchPhotosByTag("abc", 5);
                Assert.AreEqual(5, collection.Count);
            }
        }

        [TestMethod]
        public void FlickrGalleryRepository_Can_Search_And_Order_Tags()
        {
            using (mocks.Record())
            {
                Expect.Call(configurationRepository.FickerUserName).Return("userName");
                Expect.Call(flickrAPIWrapper.GetTags("userName")).Return(BuildTagList());
            }
            repository = new FlickrGalleryRepository(configurationRepository, flickrAPIWrapper);

            using (mocks.Playback())
            {
                StringCollection collection = repository.TagList("Ta");
                Assert.AreEqual(2, collection.Count);
                Assert.AreEqual("Tag1",collection[0]);
            }
        }

        private IEnumerable<string> BuildTagList()
        {
            string[] tags = new string[5] { "Tag5", "Tag1", "Snow", "Sea", "Beach" };
            return tags;
        }

        private Photoset BuildPhotoset(int size)
        {
            Photoset photoset = new Photoset();
            photoset.PhotoCollection = GetPhotoArr(size);
            return photoset;
        }

        private static Photosets BuildPhotoSetList(int size)
        {
            Photosets photosets = new Photosets();
            photosets.PhotosetCollection = new Photoset[size];
            for (int i = 0; i < size; i++)
            {
                photosets.PhotosetCollection[i] = new Photoset();
            }
            return photosets;
        }

        private static Photos BuildPhotoList(int size)
        {
            Photos photos = new Photos();
            photos.PhotoCollection = GetPhotoArr(size);
            return photos;
        }

        private static Photo[] GetPhotoArr(int size)
        {
            Photo[] photoArr= new Photo[size];
            for (int i = 0; i < size; i++)
            {
                photoArr[i] = new Photo() { PhotoId = "123" };
            }
            return photoArr;
        }
    }
}
